package com.ruoyi.web.controller.system.fuw.wx;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.fuw.domain.*;
import com.ruoyi.fuw.service.*;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.web.controller.util.DataUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

/**
 * Wx医疗服务信息Controller
 *
 * @author zjh
 * @date 2021-11-29
 */
@RestController
@RequestMapping("/api/wx/service")
public class WxFuwServiceController extends BaseController {

    @Autowired
    private IFuwServiceService fuwServiceService;

    @Autowired
    private ISysUserService userService;

    @Autowired
    private IFuwTasksService fuwTasksService;


    /**
     * 查询医疗服务列表
     */
    @PreAuthorize("@ss.hasPermi('fuw:service:list')")
    @GetMapping("/list")
    public AjaxResult list(FuwService fuwService)
    {
        // 获取当前登录用户信息,查询对应医疗服务信息
        fuwService.setPersonId(getUsername());
        return AjaxResult.success(fuwServiceService.selectFuwServiceList(fuwService));
    }


    /**
     * 新增医疗服务
     */
    @PreAuthorize("@ss.hasPermi('fuw:service:add')")
    @Log(title = "医疗服务", businessType = BusinessType.INSERT)
    @PostMapping(value = "/add/addService")
    public AjaxResult addService(@RequestParam("tasksName") String tasksName,
                                 @RequestParam("tasksType") String tasksType)
    {
        FuwService fuwService = new FuwService();

        // 自定义格式UI
        String uuidStr = DataUtils.fuwUUID("Ser");
        fuwService.setServiceId(uuidStr);

        // 根据当天任务的权重排序算法和空闲时间的医疗服务人员，进行任务分配
        SysUser sysUser = new SysUser();

        // 1、查询全部空闲状态的医疗工作者信息
        SysUser sysUserStatus = new SysUser();
        sysUserStatus.setDeptId(113L);
        sysUserStatus.setStatus("0");
        List<SysUser> userList = userService.selectList(sysUserStatus);

        // 2、通过查询当日每人未来的任务距离时间以及对应任务的权重进行计算分析
        Long numNow = Long.valueOf(0);
        for(SysUser user: userList){
            System.out.println("当前人员: " + user.getNickName());
            // 2-1、查询医务人员任务列表
            FuwTasks fuwTasks = new FuwTasks();
            fuwTasks.setTasksWeek(DateUtils.parseDate(DateUtils.getDate()));
            List<FuwTasks> fuwTasksList = fuwTasksService.selectFuwTasksList(fuwTasks);

            // 2-2、计算当前权重和时间差的乘积
            if(sysUser != null){
                if(fuwTasksList.size() > 0){
                    // 2-2、遍历医务人员的任务列表，判断医务人员是否处于任务中
                    for(FuwTasks tasks: fuwTasksList){
                        // 计算当前时间戳差
                        Long nowTime = DateUtils.parseDate(DateUtils.getTime()).getTime();
                        Long startTime = tasks.getTasksStartTime().getTime();
                        Long endTime = tasks.getTasksEndTime().getTime();
                        if((startTime <= nowTime) && (nowTime < endTime)){
                            continue;
                        }else if(startTime >= nowTime){
                            // 计算权重比
                            Long num = (startTime - nowTime) * tasks.getTasksNum();
                            System.out.println("权重: " + num);
                            if(num < numNow){
                                sysUser = user;
                            }
                        }
                    }
                }else{
                    sysUser = user;
                }
            }else{
                sysUser = user;
            }
        }

        fuwService.setServiceName(tasksName);
        fuwService.setMedicalId(sysUser.getUserName());
        fuwService.setPersonId(getUsername());
        fuwService.setServiceType(tasksType);
        fuwService.setServiceText("患者/用户微信请求医疗服务 + 用户填写内容或其他");
        fuwService.setServiceState("1");
        fuwServiceService.insertFuwService(fuwService);

        return AjaxResult.success(fuwService);
    }

}
